package auth

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"goweb/ext/app"
	"goweb/ext/e"
	"goweb/ext/jwt"
	"goweb/ext/settings"
	"goweb/service/auth"
	"goweb/utils/commons"
	"goweb/utils/redis"

	"github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

func Auth(c *gin.Context) {

	var appG = app.Gin{C: c}
	username := c.PostForm("username")
	password := c.PostForm("password")
	source := c.PostForm("source")
	var err error

	parseInt := commons.IsUserLen(source, -1)
	if !parseInt {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
		return
	}

	ok := commons.IsUserLen(password, 5)

	if !ok {
		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
		return
	}
	authService := auth.User{}
	authService.UserName = username
	authService.Password = commons.EncodeMd5(password)
	//sourceId, uTime, uid, flag, m
	sourceId, uTime, uid, flag, code, m := authService.Auth(source)
	//	//	//authService := auth.User{}
	//	//	//authService.Uid = uid

	//source, uTime, flag := authService.Check()
	//code := e.SUCCESS
	if !flag {
		appG.Response(http.StatusOK, code, nil)
		return
	}
	token, _ := redis.GetValue(commons.StringJoin([]string{conf.PERX_TOKEN, uid, "_", sourceId}), conf.RDb1)
	if token == "" {
		token, err = jwt.GenerateToken(uid, uTime, sourceId)

		if err != nil {
			appG.Response(http.StatusOK, e.UID_NOT_EXIST, nil)
			return
		}
		redis.SetValue(commons.StringJoin([]string{conf.PERX_TOKEN, uid, "_", sourceId}), token, 60*60*3, conf.RDb1)
		/**
		用户登录设置token
		*/
	}

	//这里存在问题 TODO
	/**
	解决第三方用户不会给web界面访问的url权限
	*/
	data := map[string]interface{}{"per": m, "token": token[1 : len(token)-1]}

	if !commons.CheckErr(err) {
		code = e.ERROR
	}
	appG.Response(http.StatusOK, code, data)

}

func AddUser(c *gin.Context) {
	var appG = app.Gin{C: c}
	username := c.PostForm("username")
	password := c.PostForm("password")
	source := c.PostForm("source")

	ok := commons.IsUserLen(username, 5) && commons.IsUserLen(password, 5)
	//-1表示不存在
	parseInt := commons.IsUserLen(source, 16)

	if !ok || !parseInt {
		println(1111)
		appG.Response(http.StatusOK, e.INVALID_LEN, nil)
		return
	}
	user := auth.User{}
	user.UserName = username
	user.Password = commons.EncodeMd5(password)
	//user.Source = parseInt
	if user.AddUser(source) {
		appG.Response(http.StatusOK, e.SUCCESS, username)
	} else {
		appG.Response(http.StatusOK, e.ERROR_EXIST_TAG, nil)
	}

}

func UpdateUser(c *gin.Context) {

	var appG = app.Gin{C: c}
	uid := c.PostForm("uid")
	username := c.PostForm("username")
	password := c.PostForm("password")
	user := auth.User{}
	code := e.SUCCESS
	if commons.IsUserLen(username, 5) && commons.IsUserLen(uid, 5) && commons.IsUserLen(password, 5) {
		user.UserName = username
		user.Password = commons.EncodeMd5(password)
		user.Uid = uid

		if !user.UpdateUser() {
			code = e.ERROR
		}

	}
	appG.Response(http.StatusOK, code, nil)
}

/**
添加角色
*/
func AddRole(c *gin.Context) {
	var appG = app.Gin{C: c}
	id := c.PostForm("roleId")
	name := c.PostForm("name")

	role := auth.Role{}
	role.RoleId = id
	code := e.SUCCESS
	if role.ExistRoleByID() {
		code = e.ERROR_EXIST_TAG
	} else {
		role.Name = name
		if !role.AddRole() {
			code = e.INSTALL_ERROR
		}
	}
	appG.Response(http.StatusOK, code, nil)

}

/**
修改权限

1:修改用户角色
2:修改用户权限
{先要获取该用户的权限列表}
*/

func UpdatePer(c *gin.Context) {

}

/**
获取用户权限
*/
func GetUserPer(c *gin.Context) {
	var appG = app.Gin{C: c}
	uid := c.Query("uid")
	code := e.SUCCESS

	user := auth.User{}
	user.Uid = uid
	if commons.IsUserLen(uid, 1) {
		if user.ExistUserByUid() == conf.STRING_FLAG {
			code = e.ERROR_NOT_EXIST_TAG
		} else {
			if per, data := user.GetUserPer(); per {
				appG.Response(http.StatusOK, code, data)
				return
			} else {
				code = e.GET_ERROR
			}
		}
	}
	appG.Response(http.StatusOK, code, nil)
}

/**
修改用户权限
1:用户uid
2:用户权限
3:给的权限id{获取所有权限列表}

role user_role user_per role_per per
*/
/**
单独修改角色
*/

func UpdateUserPer(c *gin.Context) {
	var appG = app.Gin{C: c}
	uid := c.PostForm("uid")
	perId := c.PostForm("perId")
	url := c.PostForm("url")
	roleId := c.PostForm("roleId")
	roleName := c.PostForm("name")
	r := auth.Role{}
	r.RoleId = roleId
	r.Name = roleName
	per := r.UpdateUserPer(uid, perId, url)
	code := e.SUCCESS
	if !per {
		code = e.ERROR
	}
	appG.Response(http.StatusOK, code, nil)
}

/**
获取所有权限列表
*/
func GetAllPer(c *gin.Context) {
	var appG = app.Gin{C: c}
	role := auth.Per{}
	per, b := role.GetAllPer()
	code := e.SUCCESS
	if !b {
		code = e.GET_ERROR
	} else if len(per) == 0 {
		code = e.DATA_NOT_EXIST
	}

	/*
		信息缓存到redis
	*/
	appG.Response(http.StatusOK, code, per)

}

/**
用户退出
*/
func Logout(c *gin.Context) {
	var appG = app.Gin{C: c}
	token := c.Query("token")
	code := e.SUCCESS
	if commons.IsUserLen(token, 100) {
		claims, err, _ := jwt.ParseToken(token)
		if !commons.CheckErr(err) {
			code = e.ERROR_TOKEN
		} else {
			redis.Delete(conf.PERX_TOKEN + claims.Uid)
		}
	} else {
		code = e.ERROR_AUTH
	}
	appG.Response(http.StatusOK, code, nil)

}
